//
//  Networking.m
//  aixianfeng23
//
//  Created by 黄晓冬 on 17/1/3.
//  Copyright © 2017年 Huang Xiaodong. All rights reserved.
//

#import "Networking.h"

@implementation Networking

+ (instancetype)sharedNetworking
{
    static Networking *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        
        instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://iosapi.itcast.cn/"]];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        //把默认获取的数据改为二进制
        instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 修改AFN默认的发送数据的类型,改为JOSN
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
    });
    
    return instance;
}





-(void)loadDataWithMethod:(network)method url:(NSString *)urlstr parameters:(id)parameters success:(void(^)(id responseobject))successBlock failure:(void(^)(NSError *error))failureBlock
{
    
    if (method==POST)
    {
        [self POST:urlstr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            id dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            if (successBlock)
            {
                successBlock(dict);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureBlock) {
                failureBlock(error);
            }
        }];
    }
    else
    {
        [self GET:urlstr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            id dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            if (successBlock)
            {
                successBlock(dict);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureBlock) {
                failureBlock(error);
            }
        }];
    }
}




@end

